package com.huluobo.user.view

import android.text.TextUtils
import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.lib_base.BaseTitle
import com.huluobo.lib_base.UIState
import com.huluobo.lib_http.Const
import com.huluobo.user.UserIntent
import com.huluobo.user.UserViewModel
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/4/2.
 */

@Composable
fun AddEmployeePage(navHostController: NavHostController, vm: UserViewModel = hiltViewModel()) {
    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.OnError -> {
                        Log.e("TagA", "添加员工失败:${it.msg}")
                    }

                    UIState.OnLoading -> {}
                    is UIState.OnSuccess<*> -> {
                        ToastUtils.showLong("添加员工成功")
                        navHostController.popBackStack()
                    }
                }
            }
        }
    })

    var name by remember {
        mutableStateOf("")
    }

    var no by remember {
        mutableStateOf("")
    }

    var phone by remember {
        mutableStateOf("")
    }

    var did by remember {
        mutableStateOf("")
    }

    var pid by remember {
        mutableStateOf("")
    }

    var pname by remember {
        mutableStateOf("")
    }

    Column(verticalArrangement = Arrangement.spacedBy(20.dp), modifier = Modifier.padding(20.dp)) {
        BaseTitle(title = "添加员工", navHostController = navHostController)
        Spacer(modifier = Modifier.height(20.dp))
        OutlinedTextField(
            value = name,
            onValueChange = { name = it },
            Modifier.fillMaxWidth(),
            label = { Text(text = "昵称") }, leadingIcon = {
                Icon(imageVector = Icons.Default.AccountCircle, contentDescription = "")
            })
        OutlinedTextField(
            value = no,
            onValueChange = { no = it },
            Modifier.fillMaxWidth(),
            label = { Text(text = "登录名") }, leadingIcon = {
                Icon(imageVector = Icons.Default.AccountCircle, contentDescription = "")
            })
        OutlinedTextField(
            value = phone,
            onValueChange = { phone = it },
            Modifier.fillMaxWidth(),
            label = { Text(text = "密码") }, leadingIcon = {
                Icon(imageVector = Icons.Default.AccountCircle, contentDescription = "")
            }, visualTransformation = PasswordVisualTransformation()
        )
        OutlinedTextField(
            value = did,
            onValueChange = { did = it },
            Modifier.fillMaxWidth(),
            label = { Text(text = "部门编号") }, leadingIcon = {
                Icon(imageVector = Icons.Default.AccountCircle, contentDescription = "")
            })
        OutlinedTextField(
            value = pid,
            onValueChange = { pid = it },
            Modifier.fillMaxWidth(),
            label = { Text(text = "职位编号") }, leadingIcon = {
                Icon(imageVector = Icons.Default.AccountCircle, contentDescription = "")
            })
        OutlinedTextField(
            value = pname,
            onValueChange = { pname = it },
            Modifier.fillMaxWidth(),
            label = { Text(text = "职位名称") }, leadingIcon = {
                Icon(imageVector = Icons.Default.AccountCircle, contentDescription = "")
            })

        Button(onClick = {
            if (TextUtils.isEmpty(name) ||
                TextUtils.isEmpty(no) ||
                TextUtils.isEmpty(phone) ||
                TextUtils.isEmpty(did) ||
                TextUtils.isEmpty(pid) ||
                TextUtils.isEmpty(pname)
            ) {
                ToastUtils.showLong("输入项不能为空")
                return@Button
            }

            val map = mapOf(
                Const.PARAM_ADD_EMPLOYEE_NAME to name,
                Const.PARAM_ADD_EMPLOYEE_NO to no,
                Const.PARAM_ADD_EMPLOYEE_PHONE to phone,
                Const.PARAM_ADD_EMPLOYEE_DID to did,
                Const.PARAM_ADD_EMPLOYEE_PID to pid,
                Const.PARAM_ADD_EMPLOYEE_P_NAME to pname
            )
            vm.sendIntent(UserIntent.AddEmployee(map))
        }) {
            Text(text = "添加员工")
        }
    }
}